package com.ex.fileupdown.util;

/*
 * Copyright 2001-2006 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the ";License&quot;);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS"; BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.security.SecureRandom;

/**
 * 
 * Date 占쏙옙占쏙옙釉�Util 占쎈��믭옙占�
 * @author �⑤벏�삼옙�뺥돩占쏙옙揶쏆뮆而삼옙占쏙옙�곸㉦占쏙옙
 * @since 2009.02.01
 * @version 1.0
 * @see
 *
 * <pre>
 * << 揶쏆뮇�숋옙��젾(Modification Information) >>
 *   
 *   占쎌꼷�숋옙占�    占쎌꼷�숋옙占�         占쎌꼷�숋옙�곸뒠
 *  -------    --------    ---------------------------
 *   2009.02.01  占쎈똻夷뤄옙占�        筌ㅼ뮇��占쎌빘苑�
 *
 * </pre>
 */
public class EgovDateUtil {

	/**
	 * <p>yyyyMMdd 占쎈�占�yyyy-MM-dd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙�곸뱽 占쎈굝��獄쏆룇釉�占쏙옙 占쏙옙 占쎌눘��
	 * 筌앹빓而㏆옙�뺣뼄. 占쏙옙 占쏙옙 占쎌눘占�揶쏉옙而㏆옙占쏙옙�롳옙 占쎌꼶占쏙옙�렽� 占쎈슣�붺몴占쏙옙�낆젾占쏙옙野껋럩��揶쏅�釉놂옙占�/p>
	 * 
	 * <pre>
	 * DateUtil.addYearMonthDay("19810828", 0, 0, 19)  = "19810916"
	 * DateUtil.addYearMonthDay("20060228", 0, 0, -10) = "20060218"
	 * DateUtil.addYearMonthDay("20060228", 0, 0, 10)  = "20060310"
	 * DateUtil.addYearMonthDay("20060228", 0, 0, 32)  = "20060401"
	 * DateUtil.addYearMonthDay("20050331", 0, -1, 0)  = "20050228"
	 * DateUtil.addYearMonthDay("20050301", 0, 2, 30)  = "20050531"
	 * DateUtil.addYearMonthDay("20050301", 1, 2, 30)  = "20060531"
	 * DateUtil.addYearMonthDay("20040301", 2, 0, 0)   = "20060301"
	 * DateUtil.addYearMonthDay("20040229", 2, 0, 0)   = "20060228"
	 * DateUtil.addYearMonthDay("20040229", 2, 0, 1)   = "20060301"
	 * </pre>
	 * 
	 * @param  dateStr 占쎌쥙彛��얜챷�꾬옙占퐕yyyMMdd, yyyy-MM-dd占쏙옙占쎈벡��
	 * @param  year 揶쏉옙而㏆옙占쏙옙占�0占쏙옙占쎈굝�곤옙占썲칰�뚯뒭 揶쏉옙而㏆옙占쏙옙�용뼄
	 * @param  month 揶쏉옙而㏆옙占쏙옙占�0占쏙옙占쎈굝�곤옙占썲칰�뚯뒭 揶쏉옙而㏆옙占쏙옙�용뼄
	 * @param  day 揶쏉옙而㏆옙占쏙옙占�0占쏙옙占쎈굝�곤옙占썲칰�뚯뒭 揶쏉옙而㏆옙占쏙옙�용뼄
	 * @return  yyyyMMdd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙占�	 * @throws IllegalArgumentException 占쎌쥙彛�占싼됥럫占쏙옙占쎈벤鍮먲쭪占썼쳸遺울옙 占썬끇占�野껋럩�� 
	 *         占쎈굝��揶쏅���<code>null</code>占쏙옙野껋럩��
	 */
	public static String addYearMonthDay(String sDate, int year, int month, int day) {

		String dateStr = validChkDate(sDate);

		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
		try {
			cal.setTime(sdf.parse(dateStr));
		} catch (ParseException e) {
			throw new IllegalArgumentException("Invalid date format: " + dateStr);
		}

		if (year != 0) {
			cal.add(Calendar.YEAR, year);
		}
		if (month != 0) {
			cal.add(Calendar.MONTH, month);
		}
		if (day != 0) {
			cal.add(Calendar.DATE, day);
		}
		return sdf.format(cal.getTime());
	}

	/**
	 * <p>yyyyMMdd 占쎈�占�yyyy-MM-dd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙�곸뱽 占쎈굝��獄쏆룇釉�占쎄쑴��
	 * 筌앹빓而㏆옙�뺣뼄. <code>year</code>占쏙옙揶쏉옙而㏆옙占쏙옙�롳옙 占쎌꼶占쏙옙�렽� 占쎈슣�붺몴占쏙옙�낆젾占쏙옙野껋럩��揶쏅�釉놂옙占�/p>
	 * 
	 * <pre>
	 * DateUtil.addYear("20000201", 62)  = "20620201"
	 * DateUtil.addYear("20620201", -62) = "20000201"
	 * DateUtil.addYear("20040229", 2)   = "20060228"
	 * DateUtil.addYear("20060228", -2)  = "20040228"
	 * DateUtil.addYear("19000101", 200) = "21000101"
	 * </pre>
	 * 
	 * @param  dateStr 占쎌쥙彛��얜챷�꾬옙占퐕yyyMMdd, yyyy-MM-dd占쏙옙占쎈벡��
	 * @param  year 揶쏉옙而㏆옙占쏙옙占�0占쏙옙占쎈굝�곤옙占썲칰�뚯뒭 揶쏉옙而㏆옙占쏙옙�용뼄
	 * @return  yyyyMMdd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙占�	 * @throws IllegalArgumentException 占쎌쥙彛�占싼됥럫占쏙옙占쎈벤鍮먲쭪占썼쳸遺울옙 占썬끇占�野껋럩�� 
	 *         占쎈굝��揶쏅���<code>null</code>占쏙옙野껋럩��
	 */
	public static String addYear(String dateStr, int year) {
		return addYearMonthDay(dateStr, year, 0, 0);
	}

	/**
	 * <p>yyyyMMdd 占쎈�占�yyyy-MM-dd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙�곸뱽 占쎈굝��獄쏆룇釉�占쎈뗄��
	 * 筌앹빓而㏆옙�뺣뼄. <code>month</code>占쏙옙揶쏉옙而㏆옙占쏙옙�롳옙 占쎌꼶占쏙옙�렽� 占쎈슣�붺몴占쏙옙�낆젾占쏙옙野껋럩��揶쏅�釉놂옙占�/p>
	 * 
	 * <pre>
	 * DateUtil.addMonth("20010201", 12)  = "20020201"
	 * DateUtil.addMonth("19800229", 12)  = "19810228"
	 * DateUtil.addMonth("20040229", 12)  = "20050228"
	 * DateUtil.addMonth("20050228", -12) = "20040228"
	 * DateUtil.addMonth("20060131", 1)   = "20060228"
	 * DateUtil.addMonth("20060228", -1)  = "20060128"
	 * </pre>
	 * 
	 * @param  dateStr 占쎌쥙彛��얜챷�꾬옙占퐕yyyMMdd, yyyy-MM-dd占쏙옙占쎈벡��
	 * @param  month 揶쏉옙而㏆옙占쏙옙占�0占쏙옙占쎈굝�곤옙占썲칰�뚯뒭 揶쏉옙而㏆옙占쏙옙�용뼄
	 * @return  yyyyMMdd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙占�	 * @throws IllegalArgumentException 占쎌쥙彛�占싼됥럫占쏙옙占쎈벤鍮먲쭪占썼쳸遺울옙 占썬끇占�野껋럩�� 
	 *         占쎈굝��揶쏅���<code>null</code>占쏙옙野껋럩��
	 */
	public static String addMonth(String dateStr, int month) {
		return addYearMonthDay(dateStr, 0, month, 0);
	}

	/**
	 * <p>yyyyMMdd 占쎈�占�yyyy-MM-dd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙�곸뱽 占쎈굝��獄쏆룇釉�占쏙옙day)�쒙옙 
	 * 筌앹빓而㏆옙�뺣뼄. <code>day</code>占쏙옙揶쏉옙而㏆옙占쏙옙�롳옙 占쎌꼶占쏙옙�렽� 占쎈슣�붺몴占쏙옙�낆젾占쏙옙野껋럩��揶쏅�釉놂옙占�
	 * <br/><br/>
	 * 占쎄쑴肉�占쎈벡�쏙옙占폸ddDays 筌롫뗄苑뚳옙�뺣뮉 占싼딆뒠占쎈Þ占�ParseException占쏙옙獄쏆꼶諭띰옙占쏙㎗�롡봺占쎈똻鍮�占쎌꼶���븍뜇�わ옙�μ뵠
	 * 占쎈뜃由�占쎈슢揆占쏙옙�곕떽占쏙옙占쏙쭖遺욧퐣占쎌뮇�좑옙占�/p>
	 * 
	 * <pre>
	 * DateUtil.addDay("19991201", 62) = "20000201"
	 * DateUtil.addDay("20000201", -62) = "19991201"
	 * DateUtil.addDay("20050831", 3) = "20050903"
	 * DateUtil.addDay("20050831", 3) = "20050903"
	 * // 2006占쏙옙6占쏙옙31占쎌눘占�占썬끉�ｆ에占썼��곸삺占쎌꼷占�占쎈봾��占쎌쥙彛⑨옙��뼄 -> 20060701嚥∽옙揶쏄쑴竊쒙옙�뺣뼄
	 * DateUtil.addDay("20060631", 1) = "20060702"
	 * </pre>
	 * 
	 * @param  dateStr 占쎌쥙彛��얜챷�꾬옙占퐕yyyMMdd, yyyy-MM-dd占쏙옙占쎈벡��
	 * @param  day 揶쏉옙而㏆옙占쏙옙占�0占쏙옙占쎈굝�곤옙占썲칰�뚯뒭 揶쏉옙而㏆옙占쏙옙�용뼄
	 * @return  yyyyMMdd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙占�	 * @throws IllegalArgumentException 占쎌쥙彛�占싼됥럫占쏙옙占쎈벤鍮먲쭪占썼쳸遺울옙 占썬끇占�野껋럩�� 
	 *         占쎈굝��揶쏅���<code>null</code>占쏙옙野껋럩��
	 */
	public static String addDay(String dateStr, int day) {
		return addYearMonthDay(dateStr, 0, 0, day);
	}

	/**
	 * <p>yyyyMMdd 占쎈�占�yyyy-MM-dd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙占�code>dateStr1</code>�⑨옙<code>
	 * dateStr2</code> 占싼딆뵠占쏙옙占쏙옙占쎌꼶占��닌뗫립占쏙옙<br>
	 * <code>dateStr2</code>揶쏉옙<code>dateStr1</code> 癰귣����⑥눊援�占쎌쥙彛⑨옙占썲칰�뚯뒭占쎈Ŧ��
	 * 占쎈슣�붺몴占썼쳸�묒넎占쎌뮆�� 占쎌늿�わ옙占썲칰�뚯뒭占쎈Ŧ��0占쏙옙獄쏆꼹�싷옙�뺣뼄.</p>
	 * 
	 * <pre>
	 * DateUtil.getDaysDiff("20060228","20060310") = 10
	 * DateUtil.getDaysDiff("20060101","20070101") = 365
	 * DateUtil.getDaysDiff("19990228","19990131") = -28
	 * DateUtil.getDaysDiff("20060801","20060802") = 1
	 * DateUtil.getDaysDiff("20060801","20060801") = 0
	 * </pre>
	 * 
	 * @param  dateStr1 占쎌쥙彛��얜챷�꾬옙占퐕yyyMMdd, yyyy-MM-dd占쏙옙占쎈벡��
	 * @param  dateStr2 占쎌쥙彛��얜챷�꾬옙占퐕yyyMMdd, yyyy-MM-dd占쏙옙占쎈벡��
	 * @return  占쏙옙占쏙옙筌△뫁��
	 * @throws IllegalArgumentException 占쎌쥙彛�占싼됥럫占쏙옙占쎈벤鍮먲쭪占썼쳸遺울옙 占썬끇占�野껋럩�� 
	 *         占쎈굝��揶쏅���<code>null</code>占쏙옙野껋럩��
	 */
	public static int getDaysDiff(String sDate1, String sDate2) {
		String dateStr1 = validChkDate(sDate1);
		String dateStr2 = validChkDate(sDate2);

		if (!checkDate(sDate1) || !checkDate(sDate2)) {
			throw new IllegalArgumentException("Invalid date format: args[0]=" + sDate1 + " args[1]=" + sDate2);
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());

		Date date1 = null;
		Date date2 = null;
		try {
			date1 = sdf.parse(dateStr1);
			date2 = sdf.parse(dateStr2);
		} catch (ParseException e) {
			throw new IllegalArgumentException("Invalid date format: args[0]=" + dateStr1 + " args[1]=" + dateStr2);
		}
		int days1 = (int)((date1.getTime() / 3600000) / 24);
		int days2 = (int)((date2.getTime() / 3600000) / 24);

		return days2 - days1;
	}

	/**
	 * <p>yyyyMMdd 占쎈�占�yyyy-MM-dd 占쎈벡�뉛옙占쏙옙醫롮� �얜챷�꾬옙�곸뱽 占쎈굝��獄쏆룇釉�占쎌쥚�싷옙占쏙옙醫롮�占쎈챷占�野껓옙沅�</p>
	 * 
	 * <pre>
	 * DateUtil.checkDate("1999-02-35") = false
	 * DateUtil.checkDate("2000-13-31") = false
	 * DateUtil.checkDate("2006-11-31") = false
	 * DateUtil.checkDate("2006-2-28")  = false
	 * DateUtil.checkDate("2006-2-8")   = false
	 * DateUtil.checkDate("20060228")   = true
	 * DateUtil.checkDate("2006-02-28") = true
	 * </pre>
	 * 
	 * @param  dateStr 占쎌쥙彛��얜챷�꾬옙占퐕yyyMMdd, yyyy-MM-dd占쏙옙占쎈벡��
	 * @return  占쎌쥚�싷옙占쏙옙醫롮�占쎈챷占�占싼됵옙
	 */
	public static boolean checkDate(String sDate) {
		String dateStr = validChkDate(sDate);

		String year = dateStr.substring(0, 4);
		String month = dateStr.substring(4, 6);
		String day = dateStr.substring(6);

		return checkDate(year, month, day);
	}

	/**
	 * <p>占쎈굝�곤옙占쏙옙占�占쏙옙 占쎌눘��占쎌쥚�싷옙�뽳옙 野껓옙沅�</p>
	 * 
	 * @param  year 占쎄퀡猷�
	 * @param  month 占쏙옙
	 * @param  day 占쏙옙
	 * @return  占쎌쥚�싷옙占쏙옙醫롮�占쎈챷占�占싼됵옙
	 */
	public static boolean checkDate(String year, String month, String day) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());

			Date result = formatter.parse(year + "." + month + "." + day);
			String resultStr = formatter.format(result);
			if (resultStr.equalsIgnoreCase(year + "." + month + "." + day)) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 占쎌쥙彛⑨옙類κ묶占쏙옙String占쏙옙占쎌쥙彛�占싼됥럫 獄쏉옙TimeZone占쏙옙癰귨옙瑗랃옙占썰틠�곕뮉 筌롫뗄苑뚳옙占�	 *
	 * @param  strSource       獄쏅떽占�占쎌쥙彛�String
	 * @param  fromDateFormat  疫꿸퀣�덌옙占쏙옙醫롮� 占쎈벤源�
	 * @param  toDateFormat    占쎈�釉�옙占쏙옙醫롮� 占쎈벤源�
	 * @param  strTimeZone     癰귨옙瑗랃옙占폯imeZone(""占쎈���癰귨옙瑗�占쎈뜇釉�
	 * @return  占쎈슣��String占쏙옙占쎌쥙彛�占싼됥럫占쏙옙癰귨옙瑗랃옙占폮tring
	 */
	public static String convertDate(String strSource, String fromDateFormat, String toDateFormat, String strTimeZone) {
		SimpleDateFormat simpledateformat = null;
		Date date = null;
		String defaultFromDateFormat = "";
		String defaultToDateFormat = "";

		if (EgovStringUtil.isNullToString(strSource).trim().equals("")) {
			return "";
		}
		if (EgovStringUtil.isNullToString(fromDateFormat).trim().equals("")) {
			defaultFromDateFormat = "yyyyMMddHHmmss"; // default揶쏉옙
		}
		if (EgovStringUtil.isNullToString(toDateFormat).trim().equals("")) {
			defaultToDateFormat = "yyyy-MM-dd HH:mm:ss"; // default揶쏉옙
		}

		try {
			simpledateformat = new SimpleDateFormat(defaultFromDateFormat, Locale.getDefault());
			date = simpledateformat.parse(strSource);
			if (!EgovStringUtil.isNullToString(strTimeZone).trim().equals("")) {
				simpledateformat.setTimeZone(TimeZone.getTimeZone(strTimeZone));
			}
			simpledateformat = new SimpleDateFormat(defaultToDateFormat, Locale.getDefault());
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return simpledateformat.format(date);

	}

	/**
	 * yyyyMMdd 占쎈벡�뉛옙占쏙옙醫롮��얜챷�꾬옙�곸뱽 占쎈�釉�옙占쏙㏄癒�봼占쏙옙ch)嚥∽옙筌잛눊而�占쎈슢��빳占쎈뼄<br/>
	* <pre>
	* ex) 20030405, ch(.) -> 2003.04.05
	* ex) 200304, ch(.) -> 2003.04
	* ex) 20040101,ch(/) --> 2004/01/01 嚥∽옙�귐뗪쉘
	* </pre>
	* 
	* @param date yyyyMMdd 占쎈벡�뉛옙占쏙옙醫롮��얜챷�꾬옙占�	* @param ch �닌됲뀋占쏙옙
	* @return 癰귨옙�싷옙占썼눧紐꾩쁽占쏙옙
	 */
	public static String formatDate(String sDate, String ch) {
		String dateStr = validChkDate(sDate);

		String str = dateStr.trim();
		String yyyy = "";
		String mm = "";
		String dd = "";

		if (str.length() == 8) {
			yyyy = str.substring(0, 4);
			if (yyyy.equals("0000")) {
				return "";
			}

			mm = str.substring(4, 6);
			if (mm.equals("00")) {
				return yyyy;
			}

			dd = str.substring(6, 8);
			if (dd.equals("00")) {
				return yyyy + ch + mm;
			}

			return yyyy + ch + mm + ch + dd;
		} else if (str.length() == 6) {
			yyyy = str.substring(0, 4);
			if (yyyy.equals("0000")) {
				return "";
			}

			mm = str.substring(4, 6);
			if (mm.equals("00")) {
				return yyyy;
			}

			return yyyy + ch + mm;
		} else if (str.length() == 4) {
			yyyy = str.substring(0, 4);
			if (yyyy.equals("0000")) {
				return "";
			} else {
				return yyyy;
			}
		} else {
			return "";
		}
	}

	/**
	 * HH24MISS 占쎈벡�뉛옙占쏙옙�볦퍢�얜챷�꾬옙�곸뱽 占쎈�釉�옙占쏙㏄癒�봼占쏙옙ch)嚥∽옙筌잛눊而�占쎈슢��빳占쎈뼄 <br>
	 * <pre>
	 *     ex) 151241, ch(/) -> 15/12/31
	 * </pre>
	 *
	 * @param str HH24MISS 占쎈벡�뉛옙占쏙옙�볦퍢�얜챷�꾬옙占�	 * @param ch �닌됲뀋占쏙옙
	 * @return 癰귨옙�싷옙占썼눧紐꾩쁽占쏙옙
	 */
	public static String formatTime(String sTime, String ch) {
		String timeStr = validChkTime(sTime);
		return timeStr.substring(0, 2) + ch + timeStr.substring(2, 4) + ch + timeStr.substring(4, 6);
	}

	/**
	 * 占쎄퀡猷꾤몴占쏙옙�낆젾 獄쏆룇釉�占쎈���占쎄퀡猷�2占쎈뗄��筌띾Ŋ��占쎌눘���쒙옙�얜챷�꾬옙��쨮 獄쏆꼹�싷옙�뺣뼄.
	 * 
	 * @param year
	 * @return 占쎈���占쎄퀡猷�2占쎈뗄��筌띾Ŋ��占쎌눘��
	 */
	public String leapYear(int year) {
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			return "29";
		}

		return "28";
	}

	/**
	 * <p>占쎈굝�계쳸�놅옙 占쎄퀡猷꾢첎占쏙옙�삳�占쎈챷占�占쎄쑬�띰쭪占썲칰占쎄텢占쎌뮆��</p>
	 * 
	 * <pre>
	 * DateUtil.isLeapYear(2004) = false
	 * DateUtil.isLeapYear(2005) = true
	 * DateUtil.isLeapYear(2006) = true
	 * </pre>
	 * 
	 * @param  year 占쎄퀡猷�
	 * @return  占썬끇��占싼됵옙
	 */
	public static boolean isLeapYear(int year) {
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			return false;
		}
		return true;
	}

	/**
	 * 占쎄쑴��占쎌뮄�끾묾怨쀯옙) 占쎌쥙彛⑨옙類ｋ궖�쒙옙占쎌궠�쀯옙占�                    <BR>
	 * 占쎌뮄由계린類ㅿ옙 yyyy-mm-dd                                  <BR>
	 * @return  String      yyyymmdd占쎈벤源�옙占쏙옙袁⑹삺 占쎌뮄�낉옙�볦퍢.   <BR>
	 */
	public static String getToday() {
		return getCurrentDate("");
	}

	/**
	 * 占쎄쑴��占쎌뮄�끾묾怨쀯옙) 占쎌쥙彛⑨옙類ｋ궖�쒙옙占쎌궠�쀯옙占�                    <BR>
	 * 占쎌뮄由계린類ㅿ옙 yyyy-mm-dd                                  <BR>
	 * @return  String      yyyymmdd占쎈벤源�옙占쏙옙袁⑹삺 占쎌뮄�낉옙�볦퍢.   <BR>
	 */
	public static String getCurrentDate(String dateType) {
		Calendar aCalendar = Calendar.getInstance();

		int year = aCalendar.get(Calendar.YEAR);
		int month = aCalendar.get(Calendar.MONTH) + 1;
		int date = aCalendar.get(Calendar.DATE);
		String strDate = Integer.toString(year) + ((month < 10) ? "0" + Integer.toString(month) : Integer.toString(month)) + ((date < 10) ? "0" + Integer.toString(date) : Integer.toString(date));

		if (!"".equals(dateType)) {
			strDate = convertDate(strDate, dateType);
		}
		
		return strDate;
	}

	/**
	 * 占쎄쑴��占쎌뮄�끾묾怨쀯옙) 占쎌쥙彛⑨옙�볦퍢占쎈베�ョ몴占쏙옙�노뮉占쏙옙                <BR>
	 * 占쎌뮄由계린類ㅿ옙 yyyy-mm-dd 24hh mi ss                       <BR>
	 * @return  String      yyyymmdd占쎈벤源�옙占쏙옙袁⑹삺 占쎌뮄�낉옙�볦퍢.   <BR>
	 */
	public static String getCurrentDateTime(String dateType) {
		Calendar aCalendar = Calendar.getInstance();

		int year = aCalendar.get(Calendar.YEAR);
		int month = aCalendar.get(Calendar.MONTH) + 1;
		int date = aCalendar.get(Calendar.DATE);
		int hour = aCalendar.get(Calendar.HOUR_OF_DAY);
		int min = aCalendar.get(Calendar.MINUTE);
		int sec = aCalendar.get(Calendar.SECOND);

		String strDate = Integer.toString(year) + ((month < 10) ? "0" + Integer.toString(month) : Integer.toString(month)) + ((date < 10) ? "0" + Integer.toString(date) : Integer.toString(date)) + ((hour < 10) ? "0" + Integer.toString(hour) : Integer.toString(hour)) + ((min < 10) ? "0" + Integer.toString(min) : Integer.toString(min)) + ((sec < 10) ? "0" + Integer.toString(sec) : Integer.toString(sec));
		String strTime = ((hour < 10) ? "0" + Integer.toString(hour) : Integer.toString(hour)) + ((min < 10) ? "0" + Integer.toString(min) : Integer.toString(min)) + ((sec < 10) ? "0" + Integer.toString(sec) : Integer.toString(sec));

		if (!"".equals(dateType)) {
			strDate = convertDate(strDate, strTime, dateType);
		}

		return strDate;
	}

	/**
	 * 占쎌쥙彛⑨옙類κ묶占쏙옙String占쏙옙占쎌쥙彛�占싼됥럫筌띾슣��癰귨옙瑗랃옙占썰틠�곕뮉 筌롫뗄苑뚳옙占�	 * @param sDate 占쎌쥙彛�
	 * @param sFormatStr 占싼됂�占썬끋�껓쭕占썼눧紐꾩쁽占쏙옙
	 * @return 筌욑옙�숋옙占쏙옙醫롮�/占쎌뮄而숋옙占쏙쭪占쎌젟占쏙옙占싼됥럫占쎌눖以��곗뮆��
	 * @See Letter  Date or Time Component  Presentation  Examples  
	           G  Era designator  Text  AD  
	           y  Year  Year  1996; 96  
	           M  Month in year  Month  July; Jul; 07  
	           w  Week in year  Number  27  
	           W  Week in month  Number  2  
	           D  Day in year  Number  189  
	           d  Day in month  Number  10  
	           F  Day of week in month  Number  2  
	           E  Day in week  Text  Tuesday; Tue  
	           a  Am/pm marker  Text  PM  
	           H  Hour in day (0-23)  Number  0  
	           k  Hour in day (1-24)  Number  24  
	           K  Hour in am/pm (0-11)  Number  0  
	           h  Hour in am/pm (1-12)  Number  12  
	           m  Minute in hour  Number  30  
	           s  Second in minute  Number  55  
	           S  Millisecond  Number  978  
	           z  Time zone  General time zone  Pacific Standard Time; PST; GMT-08:00  
	           Z  Time zone  RFC 822 time zone  -0800  
	           
	            
	           
	           Date and Time Pattern  Result  
	           "yyyy.MM.dd G 'at' HH:mm:ss z"  2001.07.04 AD at 12:08:56 PDT  
	           "EEE, MMM d, ''yy"  Wed, Jul 4, '01  
	           "h:mm a"  12:08 PM  
	           "hh 'o''clock' a, zzzz"  12 o'clock PM, Pacific Daylight Time  
	           "K:mm a, z"  0:08 PM, PDT  
	           "yyyyy.MMMMM.dd GGG hh:mm aaa"  02001.July.04 AD 12:08 PM  
	           "EEE, d MMM yyyy HH:mm:ss Z"  Wed, 4 Jul 2001 12:08:56 -0700  
	           "yyMMddHHmmssZ"  010704120856-0700  
	
	 */
	public static String convertDate(String sDate, String sFormatStr) {
		String dateStr = validChkDate(sDate);

		Calendar cal = null;
		cal = Calendar.getInstance();

		cal.set(Calendar.YEAR, Integer.parseInt(dateStr.substring(0, 4)));
		cal.set(Calendar.MONTH, Integer.parseInt(dateStr.substring(4, 6)) - 1);
		cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateStr.substring(6, 8)));

		SimpleDateFormat sdf = new SimpleDateFormat(sFormatStr, Locale.ENGLISH);

		return sdf.format(cal.getTime());
	}
	
	/**
	 * 占쎌쥙彛⑨옙類κ묶占쏙옙String占쏙옙占쎌쥙彛�占싼됥럫筌띾슣��癰귨옙瑗랃옙占썰틠�곕뮉 筌롫뗄苑뚳옙占�	 * @param sDate 占쎌쥙彛�
	 * @param sTime 占쎌뮄而�
	 * @param sFormatStr 占싼됂�占썬끋�껓쭕占썼눧紐꾩쁽占쏙옙
	 * @return 筌욑옙�숋옙占쏙옙醫롮�/占쎌뮄而숋옙占쏙쭪占쎌젟占쏙옙占싼됥럫占쎌눖以��곗뮆��
	 * @See Letter  Date or Time Component  Presentation  Examples  
	           G  Era designator  Text  AD  
	           y  Year  Year  1996; 96  
	           M  Month in year  Month  July; Jul; 07  
	           w  Week in year  Number  27  
	           W  Week in month  Number  2  
	           D  Day in year  Number  189  
	           d  Day in month  Number  10  
	           F  Day of week in month  Number  2  
	           E  Day in week  Text  Tuesday; Tue  
	           a  Am/pm marker  Text  PM  
	           H  Hour in day (0-23)  Number  0  
	           k  Hour in day (1-24)  Number  24  
	           K  Hour in am/pm (0-11)  Number  0  
	           h  Hour in am/pm (1-12)  Number  12  
	           m  Minute in hour  Number  30  
	           s  Second in minute  Number  55  
	           S  Millisecond  Number  978  
	           z  Time zone  General time zone  Pacific Standard Time; PST; GMT-08:00  
	           Z  Time zone  RFC 822 time zone  -0800  
	           
	            
	           
	           Date and Time Pattern  Result  
	           "yyyy.MM.dd G 'at' HH:mm:ss z"  2001.07.04 AD at 12:08:56 PDT  
	           "EEE, MMM d, ''yy"  Wed, Jul 4, '01  
	           "h:mm a"  12:08 PM  
	           "hh 'o''clock' a, zzzz"  12 o'clock PM, Pacific Daylight Time  
	           "K:mm a, z"  0:08 PM, PDT  
	           "yyyyy.MMMMM.dd GGG hh:mm aaa"  02001.July.04 AD 12:08 PM  
	           "EEE, d MMM yyyy HH:mm:ss Z"  Wed, 4 Jul 2001 12:08:56 -0700  
	           "yyMMddHHmmssZ"  010704120856-0700  
	
	 */
	public static String convertDate(String sDate, String sTime, String sFormatStr) {
		String dateStr = validChkDate(sDate);
		String timeStr = validChkTime(sTime);

		Calendar cal = null;
		cal = Calendar.getInstance();

		cal.set(Calendar.YEAR, Integer.parseInt(dateStr.substring(0, 4)));
		cal.set(Calendar.MONTH, Integer.parseInt(dateStr.substring(4, 6)) - 1);
		cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateStr.substring(6, 8)));
		cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeStr.substring(0, 2)));
		cal.set(Calendar.MINUTE, Integer.parseInt(timeStr.substring(2, 4)));

		SimpleDateFormat sdf = new SimpleDateFormat(sFormatStr, Locale.ENGLISH);

		return sdf.format(cal.getTime());
	}

	/**
	 * 占쎈굝�계쳸�놅옙 占쎌눘��占싼딆뵠占쏙옙占쎄쑴�쏙옙占쏙옙�깆쁽�쒙옙獄쏆꼹��
	 * @param sDate1 占쎌뮇�됵옙�깆쁽
	 * @param sDate2 �ル굝利븝옙�깆쁽
	 * @return 占쎄쑴�쏙옙�깆쁽
	 */
	public static String getRandomDate(String sDate1, String sDate2) {
		String dateStr1 = validChkDate(sDate1);
		String dateStr2 = validChkDate(sDate2);

		String randomDate = null;

		int sYear, sMonth, sDay;
		int eYear, eMonth, eDay;

		sYear = Integer.parseInt(dateStr1.substring(0, 4));
		sMonth = Integer.parseInt(dateStr1.substring(4, 6));
		sDay = Integer.parseInt(dateStr1.substring(6, 8));

		eYear = Integer.parseInt(dateStr2.substring(0, 4));
		eMonth = Integer.parseInt(dateStr2.substring(4, 6));
		eDay = Integer.parseInt(dateStr2.substring(6, 8));

		GregorianCalendar beginDate = new GregorianCalendar(sYear, sMonth - 1, sDay, 0, 0);
		GregorianCalendar endDate = new GregorianCalendar(eYear, eMonth - 1, eDay, 23, 59);

		if (endDate.getTimeInMillis() < beginDate.getTimeInMillis()) {
			throw new IllegalArgumentException("Invalid input date : " + sDate1 + "~" + sDate2);
		}

		SecureRandom sr = new SecureRandom();

		long rand = ((sr.nextLong() >>> 1) % (endDate.getTimeInMillis() - beginDate.getTimeInMillis() + 1)) + beginDate.getTimeInMillis();

		GregorianCalendar cal = new GregorianCalendar();
		//SimpleDateFormat calformat = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat calformat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
		cal.setTimeInMillis(rand);
		randomDate = calformat.format(cal.getTime());

		// 占쎌뮆�↑눧紐꾩쁽占쎈�占��귐뗪쉘
		return randomDate;
	}

	/**
	 * 占쎈굝�계쳸�놅옙 占쎈뗄�わ옙占쏙옙怨론�쭗�놁뱽 �댐옙揆筌뤿굞��占쎈뗄�ゆ에占썼쳸�묒넎 
	 * @param sWeek 占쎄낮揆 占쎈뗄�わ쭗占�	 * @return �댐옙揆 占쎈뗄�わ쭗占�	 */
	public static String convertWeek(String sWeek) {
		String retStr = null;

		if (sWeek.equals("SUN")) {
			retStr = "�쇱슂��";
		} else if (sWeek.equals("MON")) {
			retStr = "�붿슂��";
		} else if (sWeek.equals("TUE")) {
			retStr = "�붿슂��";
		} else if (sWeek.equals("WED")) {
			retStr = "�섏슂��";
		} else if (sWeek.equals("THR")) {
			retStr = "紐⑹슂��";
		} else if (sWeek.equals("FRI")) {
			retStr = "湲덉슂��";
		} else if (sWeek.equals("SAT")) {
			retStr = "�좎슂��";
		}

		return retStr;
	}

	/**
	 * 占쎈굝�곤옙�깆쁽占쏙옙占쎌쥚��占싼됵옙�쒙옙占쎈벡��
	 * @param sDate 占쎌눘��
	 * @return 占쎌쥚��占싼됵옙
	 */
	public static boolean validDate(String sDate) {
		String dateStr = validChkDate(sDate);

		Calendar cal;
		boolean ret = false;

		cal = Calendar.getInstance();

		cal.set(Calendar.YEAR, Integer.parseInt(dateStr.substring(0, 4)));
		cal.set(Calendar.MONTH, Integer.parseInt(dateStr.substring(4, 6)) - 1);
		cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateStr.substring(6, 8)));

		String year = String.valueOf(cal.get(Calendar.YEAR));
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));

		String pad4Str = "0000";
		String pad2Str = "00";

		String retYear = (pad4Str + year).substring(year.length());
		String retMonth = (pad2Str + month).substring(month.length());
		String retDay = (pad2Str + day).substring(day.length());

		String retYMD = retYear + retMonth + retDay;

		if (sDate.equals(retYMD)) {
			ret = true;
		}

		return ret;
	}

	/**
	 * 占쎈굝�곤옙�깆쁽, 占쎈뗄�わ옙占쏙옙醫륁뒞 占싼됵옙�쒙옙占쎈벡��
	 * @param     sDate 占쎌눘��
	 * @param     sWeek 占쎈뗄��(DAY_OF_WEEK)
	 * @return    占쎌쥚��占싼됵옙
	 */
	public static boolean validDate(String sDate, int sWeek) {
		String dateStr = validChkDate(sDate);

		Calendar cal;
		boolean ret = false;

		cal = Calendar.getInstance();

		cal.set(Calendar.YEAR, Integer.parseInt(dateStr.substring(0, 4)));
		cal.set(Calendar.MONTH, Integer.parseInt(dateStr.substring(4, 6)) - 1);
		cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateStr.substring(6, 8)));

		int week = cal.get(Calendar.DAY_OF_WEEK);

		if (validDate(sDate)) {
			if (sWeek == week) {
				ret = true;
			}
		}

		return ret;
	}

	/**
	 * 占쎈굝�곤옙�볦퍢占쏙옙占쎌쥚��占싼됵옙�쒙옙占쎈벡��
	 * @param     sTime 占쎈굝�곤옙�볦퍢
	 * @return    占쎌쥚��占싼됵옙
	 */
	public static boolean validTime(String sTime) {
		String timeStr = validChkTime(sTime);

		Calendar cal;
		boolean ret = false;

		cal = Calendar.getInstance();

		cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeStr.substring(0, 2)));
		cal.set(Calendar.MINUTE, Integer.parseInt(timeStr.substring(2, 4)));

		String hh = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
		String mm = String.valueOf(cal.get(Calendar.MINUTE));

		String pad2Str = "00";

		String retHH = (pad2Str + hh).substring(hh.length());
		String retMM = (pad2Str + mm).substring(mm.length());

		String retTime = retHH + retMM;

		if (sTime.equals(retTime)) {
			ret = true;
		}

		return ret;
	}

	/**
	 * 占쎈굝�곤옙占쏙옙�깆쁽占쏙옙占쏙옙 占쏙옙 占쎌눘��揶쏉옙而㏆옙占쏙옙醫롮�占쏙옙占쎈뗄�わ옙占썼쳸�묒넎
	 * @param sDate 占쎌쥙彛�
	 * @param year 占쏙옙
	 * @param month 占쏙옙
	 * @param day 占쏙옙
	 * @return �④쑴沅쏉옙占쏙옙�깆쁽占쏙옙占쎈뗄��DAY_OF_WEEK)
	 */
	public static String addYMDtoWeek(String sDate, int year, int month, int day) {
		String dateStr = validChkDate(sDate);

		dateStr = addYearMonthDay(dateStr, year, month, day);

		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
		try {
			cal.setTime(sdf.parse(dateStr));
		} catch (ParseException e) {
			throw new IllegalArgumentException("Invalid date format: " + dateStr);
		}

		SimpleDateFormat rsdf = new SimpleDateFormat("E", Locale.ENGLISH);

		return rsdf.format(cal.getTime());
	}

	/**
	 * 占쎈굝�곤옙占쏙옙�깆쁽占쏙옙占쏙옙 占쏙옙 占쏙옙 占쎌뮄而� �브쑴��揶쏉옙而㏆옙占쏙옙醫롮�, 占쎌뮄而숋옙占쏙옙�캡占썬끋�껓쭕占쏙옙類ㅻ뻼占쎌눖以�獄쏆꼹��
	 * @param sDate 占쎌쥙彛�
	 * @param sTime 占쎌뮄而�
	 * @param year 占쏙옙
	 * @param month 占쏙옙
	 * @param day 占쏙옙
	 * @param hour 占쎌뮄而�
	 * @param minute �븝옙
	 * @param formatStr 占싼됂몌옙�쎈뱜筌랃옙
	 * @return
	 */
	public static String addYMDtoDayTime(String sDate, String sTime, int year, int month, int day, int hour, int minute, String formatStr) {
		String dateStr = validChkDate(sDate);
		String timeStr = validChkTime(sTime);

		dateStr = addYearMonthDay(dateStr, year, month, day);

		dateStr = convertDate(dateStr, timeStr, "yyyyMMddHHmm");

		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH);

		try {
			cal.setTime(sdf.parse(dateStr));
		} catch (ParseException e) {
			throw new IllegalArgumentException("Invalid date format: " + dateStr);
		}

		if (hour != 0) {
			cal.add(Calendar.HOUR, hour);
		}

		if (minute != 0) {
			cal.add(Calendar.MINUTE, minute);
		}

		SimpleDateFormat rsdf = new SimpleDateFormat(formatStr, Locale.ENGLISH);

		return rsdf.format(cal.getTime());
	}

	/**
	 * 占쎈굝�곤옙占쏙옙�깆쁽�쒙옙int 占쎈벡�앮에占썼쳸�묒넎
	 * @param sDate 占쎌눘��
	 * @return int(占쎌눘��
	 */
	public static int datetoInt(String sDate) {
		return Integer.parseInt(convertDate(sDate, "0000", "yyyyMMdd"));
	}

	/**
	 * 占쎈굝�곤옙占쏙옙�볦퍢占쏙옙int 占쎈벡�앮에占썼쳸�묒넎
	 * @param sTime 占쎌뮄而�
	 * @return int(占쎌뮄而�
	 */
	public static int timetoInt(String sTime) {
		return Integer.parseInt(convertDate("00000101", sTime, "HHmm"));
	}

	/**
	 * 占쎈굝�곤옙占쏙옙�깆쁽 �얜챷�꾬옙�곸뱽 占쎈벡�ㅿ옙�랁� 8占쎈Ŧ�곫에占썹뵳�苑�  
	 * @param sDate
	 * @return
	 */
	public static String validChkDate(String dateStr) {
		String rtnDateStr = dateStr;

		if (dateStr == null || !(dateStr.trim().length() == 8 || dateStr.trim().length() == 10)) {
			throw new IllegalArgumentException("Invalid date format: " + dateStr);
		}
		if (dateStr.length() == 10) {
			rtnDateStr = EgovStringUtil.removeMinusChar(dateStr);
		}
		return rtnDateStr;
	}

	/**
	 * 占쎈굝�곤옙占쏙옙�깆쁽 �얜챷�꾬옙�곸뱽 占쎈벡�ㅿ옙�랁� 8占쎈Ŧ�곫에占썹뵳�苑�  
	 * @param sDate
	 * @return
	 */
	public static String validChkTime(String timeStr) {
		String rtnTimeStr = timeStr;

		if (rtnTimeStr.length() == 5) {
			rtnTimeStr = EgovStringUtil.remove(rtnTimeStr, ':');
		}
		if (rtnTimeStr == null || !(rtnTimeStr.trim().length() == 4)) {
			throw new IllegalArgumentException("Invalid time format: " + rtnTimeStr);
		}

		return rtnTimeStr;
	}

}
